//
//  BookDetailViewController.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/7/23.
//

import UIKit
import Toast

class BookDetailViewController: BaseViewController, UITextViewDelegate {
    
    @IBOutlet weak var bookImage: UIImageView!
    @IBOutlet weak var bookName: UILabel!
    @IBOutlet weak var bookAuthor: UILabel!
    @IBOutlet weak var bookShiyan: UILabel!
    @IBOutlet weak var xindeTitle: UILabel!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var finishBtn: UIButton!
    
    private var bookXinde: NSString = ""//阅读心得
    @IBOutlet weak var hasRead: UILabel!
    @IBOutlet weak var slider: UISlider!
    
    var model: BookModel?
    
    var backEnve : backEnveClosureType?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupData()
    }

    private func setupData(){
        if let imageStr = model?.imageStr {
            if let typeStr = model?.imageType {
                if Int(typeStr) == 1 {
                    if let savedImage = String.convertStrToImage(imageStr) {
                        bookImage.image = savedImage
                    }
                } else {
                    bookImage.image = UIImage(named: imageStr)
                }
            }
        }
        navigationItem.title = model!.book_title ?? ""
        bookName.text = model!.book_title ?? ""
        bookAuthor.text = model!.book_author ?? ""
        bookShiyan.text = model!.book_shiyan ?? ""
        textView.text = model!.book_xinde ?? ""
        slider.isEnabled = true
        if Int(model!.status!) == 1 {
            xindeTitle.text = "写下你的读书心得："
            textView.isEditable = true
//            finishBtn.isHidden = false
            textView.backgroundColor = .white
            textView.textColor = kColor333333
            
            if let progress = model?.progress {
                let proFloat = Float(progress) ?? 0.0
                hasRead.text = "读书记录：已阅读\(Int(proFloat*100))%"
                slider.value = proFloat
                slider.isEnabled = true
            }
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "更新阅读进度", style: .plain, target: self, action: #selector(rightAction))

        } else {
            xindeTitle.text = "读书心得"
            textView.isEditable = false
//            finishBtn.isHidden = true
            textView.backgroundColor = .clear
            textView.textColor = kThemeColor
            hasRead.text = "读书记录：已阅读100%"
            slider.value = 1.0
            slider.isEnabled = false
        }
    }
    
//    func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
//        return true
//    }

    func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
        textView.endEditing(true)
        return true
    }

//    func textViewDidBeginEditing(_ textView: UITextView) {}
//
//    func textViewDidEndEditing(_ textView: UITextView) {}

    

    func textViewDidChange(_ textView: UITextView) {
        self.bookXinde = (textView.text ?? "") as NSString
    }

    
    
    
    
    @IBAction func submitAction(_ sender: UIButton) {
        view.endEditing(true)
        
//        if self.bookXinde.length == 0 {
//            UIWindow().currentWindow()?.makeToast("请填写读书心得", duration: 1.0, position: CSToastPositionCenter)
//            return
//        }
        model?.progress = String(slider.value)
        model!.book_xinde = String(self.bookXinde)
        if slider.value == 1.0 {
            model!.status = "2"
            LocalData().saveFinishBookInfoModel(infoModel: model!)
            LocalData().deleteAddBook(book_id: model!.book_id!)
        } else {
            model!.status = "1"
            LocalData().saveAddBookInfoModel(infoModel: model!)
        }
        
        if self.backEnve != nil {
            self.backEnve!()
        }
        navigationController?.popViewController(animated: true)
    }
    
    @IBAction func sliderAction(_ sender: UISlider) {
        hasRead.text = "读书记录：已阅读\(Int(sender.value*100))%"
    }
    
    @objc func rightAction() {
        model?.progress = String(slider.value)
        model!.book_xinde = String(self.bookXinde)
        if slider.value == 1.0 {
            model!.status = "2"
            LocalData().saveFinishBookInfoModel(infoModel: model!)
            LocalData().deleteAddBook(book_id: model!.book_id!)
        } else {
            model!.status = "1"
            LocalData().saveAddBookInfoModel(infoModel: model!)
        }
        
        if self.backEnve != nil {
            self.backEnve!()
        }
        navigationController?.popViewController(animated: true)
    }
    
}
